package com.ybkj.daijia.server.amqp.rabbitTemplate;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 消息队列模板：用于发送和接受消息
 *
 * @author jacklee
 */
public class BaseTemplate {


    protected Channel channel;
    private Connection connection;


    public BaseTemplate() {

    }

    public BaseTemplate(String AMQP_HOST, String AMQP_PORT, String AMQP_USERNAME,
        String AMQP_PASSWORD, String AMQP_SP_EXCHANGE) {

        try {
            // Create a connection factory
            ConnectionFactory factory = new ConnectionFactory();

            // hostname of your rabbitmq server
            factory.setHost(AMQP_HOST);
            factory.setUsername(AMQP_USERNAME);
            factory.setPassword(AMQP_PASSWORD);
            if (AMQP_PORT != null) {
                factory.setPort(Integer.valueOf(AMQP_PORT));
            }

            connection = factory.newConnection();

            // creating a channel
            channel = connection.createChannel();
            //channel.exchangeDeclare(AMQP_SP_EXCHANGE, "fanout",true);

            // declaring a queue for this channel. If queue does not exist,
            // it will be created on the server.
            //channel.queueDeclare(QUEUE, false, false, false, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
